<?php
#同步绑定数据（用于淄博）
$token='08b8cc14d692bdd2e1f823ebe3c4b54e';
define('IN_MOBILE', true);

$data = $_GET;

if(!$data){
    response(10001,'请求参数为空');
}

#检测参数,验签
check_data($data);

#检测函数有效性

#配置

require '../framework/bootstrap.inc.php';
load()->app('common');
load()->app('template');

$function = $data['service'];
$function($data);



/** 绑定同步
 * @param $info
 * @return string
 */
function binding_add($info) {

    $multi = pdo_get('zhtc_user',array('openid'=>$info['openid']));
    if(!$multi){
        response(10001,'绑定失败,用户不存在');
    }
    #修改user表绑定桩体
    $user_flag = true;
    if(!$multi['binding_status']){
        $user_flag = pdo_update('zhtc_user', array('binding_status'=>1), array('id' => $multi['id']));
    }
    #录入绑定表
    $save_data = array(
        'uid' => $multi['id'],
        'openid' => $multi['openid'],
        'zb_uid' => $info['user_id'],
        'mobile' => $info['mobile'],
        'login_password' => $info['login_password'],
        'createtime' => time(),
    );
    $flag = pdo_delete('zhtc_user_binding', array('uid' => $multi['id']));
    $binding_flag = pdo_insert('zhtc_user_binding',$save_data);
    if($user_flag && $binding_flag){
        response(10000,'成功');
    }else{
        response(10001,'绑定失败');
    }
}

/** 解除绑定同步
 * @param $info
 * @return string
 */
function binding_cancel($info) {
    $multi = pdo_get('zhtc_user',array('openid'=>$info['openid']));
    if(!$multi){
        response(10001,'解绑失败,用户不存在');
    }
    #修改user表绑定桩体
    $user_flag = true;
    if($multi['binding_status']){
        $user_flag = pdo_update('zhtc_user', array('binding_status'=>0), array('id' => $multi['id']));
    }
    #删除绑定表
    $binding_flag = true;
    $user_binding = pdo_get('zhtc_user_binding',array('uid'=> $multi['id']));
    if($user_binding){
        $binding_flag = pdo_delete('zhtc_user_binding', array('uid' => $multi['id']));
    }

    if($user_flag && $binding_flag){
        response(10000,'成功');
    }else{
        response(10001,'解除绑定失败');
    }
}

function check_data($data) {
    if(!$data){
        response(10001,'参数缺失');
    }
//    bc92d1643dbf7947b6cc8f266b4162dd
    $sign = $data['sign'];
    unset($data['sign']);
    $check_sign = gen_sign($data);
    if($sign != $check_sign){
        response(10001,'验签失败');
    }
}

function gen_sign($params) {
    global $token;
    ksort($params);
    $paramsStrExceptSign = '';
    foreach ($params as $val) {
        $paramsStrExceptSign .= trim($val);
    }
    // error_log(var_export($params,1),3,RUNTIME_PATH.'/sign.txt');
    return md5(md5($paramsStrExceptSign).$token);
}

function response( $code = 10000, $msg = '',$data = [])
{
    $response = [
        'ret' => 200, // 服务器层状态码
    ];
    $response['data']['code'] = $code;
    if ($code == 0) {
        // $response['data']['data'] = $data;
        if (isset($data['list'])) {
            //$response['data'] = $data;
            $response['data']['code'] = $code;
            $response['data']['list'] = $data['list'];
            if ($data['pageinfo']) {
                $response['data']['pageinfo'] = $data['pageinfo'];
            }
            if ($data['page_info']) {
                $response['data']['page_info'] = $data['page_info'];
            }
            if ($data['total_fee']) {
                $response['data']['total_fee'] = $data['total_fee'];
            }
            if ($data['config']) {
                $response['data']['config'] = $data['config'];
            }
        } else {
            $response['data']['data'] = $data;
        }
    }

    if ($msg) {
        $response['data']['msg'] = $msg;
    }
    echo json_encode($response);exit;
}


//#sal
//CREATE TABLE `ims_zhtc_user_binding` (
//`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '表id',
//  `uid` int(11) DEFAULT NULL COMMENT '会员id',
//  `openid` varchar(100) DEFAULT NULL COMMENT '会员openid',
//  `zb_uid` int(11) DEFAULT NULL COMMENT '淄博会员id',
//  `mobile` varchar(30) DEFAULT NULL COMMENT '手机号',
//  `login_password` varchar(100) DEFAULT NULL COMMENT '登录密码',
//  `createtime` int(15) DEFAULT NULL COMMENT '创建时间',
//  PRIMARY KEY (`id`)
//) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='会员绑定表';

//alter table `ims_zhtc_user` add column binding_status int(2) DEFAULT NULL COMMENT '绑定状态';